import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/*
 *@Student 张策
 */
public class Main {
    private static JdbcTemplate jdb = new JdbcTemplate(JDBCUtils.getDataSource());
    public static void main(String[] args) {
        String sql="UPDATE acc set money=? WHERE id=?";
        jdb.update(sql,99997,12);
    }
    @Test
    public void text(){
        String sql="select*from acc where id=?";
        Map<String, Object> map = jdb.queryForMap(sql,1);
        System.out.println(map);
    }
    @Test
    public void text2(){
        String sql="select*from acc";
        List<Map<String, Object>> map = jdb.queryForList(sql);
        System.out.println(map);
    }
    //    @Test
//    public void text3(){//提供了实现好的，也可以自己实现
//        String sql="select*from acc";
//        List<Acc> accs = jdb.query(sql, new RowMapper<Acc>() {
//            @Override
//            public Acc mapRow(ResultSet res, int i) throws SQLException {
//                Acc acc = new Acc();
//                int id = res.getInt("id");
//                String name = res.getString("name");
//                int money = res.getInt("money");
//                acc.setId(id);
//                acc.setName(name);
//                acc.setMoney(money);
//                return acc;
//            }
//        });
//        System.out.println(accs);
//    }
    @Test
    public void text3(){//
        String sql="select*from acc";
        List<Acc> accs = jdb.query(sql, new BeanPropertyRowMapper<Acc>(Acc.class));
        System.out.println(accs);
    }
    @Test
    public void text4(){//
        String sql="select count(id) from acc";
        Integer l = jdb.queryForObject(sql, Integer.class);
        System.out.println(l);
    }
}
class Acc{
    private Integer id;
    private String name;
    private Integer money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Acc{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}